package com.onlyphoto.onlymall.mvp.view.binder

import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.bean.response.UserInfoBean
import com.onlyphoto.onlymall.mvp.view.activity.LoginActivity
import com.onlyphoto.onlymall.mvp.view.activity.OrderManagerActivity
import com.onlyphoto.onlymall.utils.ConfigUtil

import me.drakeet.multitype.ItemViewBinder

/**
 * Created by gaochujia on 2019-07-01.
 */
class MyHeaderBinder : ItemViewBinder<UserInfoBean, MyHeaderBinder.ViewHolder>() {


    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val root = inflater.inflate(R.layout.my_head, parent, false)
        return ViewHolder(root)
    }

    override fun onBindViewHolder(viewHolder: ViewHolder, userInfo: UserInfoBean) {
        viewHolder.update(userInfo)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {

        var loginOrName: TextView? = null
        var waitPay: LinearLayout? = null
        var waitDispatch: LinearLayout? = null
        var waitReceive: LinearLayout? = null
        var all: LinearLayout? = null

        init {
            loginOrName = itemView.findViewById(R.id.login_or_name)
            waitPay = itemView.findViewById(R.id.wait_pay)
            waitDispatch = itemView.findViewById(R.id.wait_dispatch)
            waitReceive = itemView.findViewById(R.id.wait_receive)
            all = itemView.findViewById(R.id.all)

            loginOrName?.setOnClickListener(this)
            waitPay?.setOnClickListener(this)
            waitDispatch?.setOnClickListener(this)
            waitReceive?.setOnClickListener(this)
            all?.setOnClickListener(this)
        }

        override fun onClick(v: View?) {
            var status = 0
            when (v?.id) {
                R.id.login_or_name -> {
                    itemView.context?.startActivity(Intent(itemView.context, LoginActivity::class.java))
                    return
                }
                R.id.wait_pay -> {
                    status = 1
                }
                R.id.wait_dispatch -> {
                    status = 2
                }
                R.id.wait_receive -> {
                    status = 3
                }
                R.id.all -> {
                    status = 0
                }
            }
            if (!ConfigUtil.instance.loginStatus) {
                itemView.context?.startActivity(Intent(itemView.context,LoginActivity::class.java))
                return
            }
            itemView.context?.startActivity(Intent(itemView.context, OrderManagerActivity::class.java)
                    .putExtra("status", status))
        }

        fun update(userInfo: UserInfoBean) {
            if (userInfo.nickname != "") {
                loginOrName?.text = userInfo.nickname
                loginOrName?.isEnabled = false
            } else {
                loginOrName?.text = "登录/注册"
                loginOrName?.isEnabled = true
            }
        }
    }
}
